<?php

class Admin_MusicController extends Yaf_Controller_Abstract {

    private $_layout;
    private $sign = array();
    private $request = array();

    public function init() {
        $this->sign = isset($_SESSION['info']) ? $_SESSION['info'] : "";
        $this->request = $this->getRequest();
        if (empty($this->sign)) {
            header("Location:/admin_login");
            exit;
        }
        /* use layout */
        $this->_dispatcher = Yaf_Registry::get("dispatcher");
        $this->_layout = new LayoutPlugin('layout.phtml');
        $this->_dispatcher->registerPlugin($this->_layout);
    }

    /*
     * show admin info
     */

    public function IndexAction() {       
        if (!strstr($this->sign['permission'], strtolower($this->request->controller) . '/' . $this->request->action)) {
            header("Content-Type:text/html;charset=utf-8");
            echo "<script>alert('无权限进行此操作，请先联系管理员！');history.back();</script>";
            exit;
        }
        $Music = new MusicModel();
        $info_list = $Music->lists();
        if ($keyword = $this->getRequest()->getPost('keyword', '')) {
            foreach ($info_list as $key => $value) {
                if (strstr($value, $keyword) === false) {
                    unset($info_list[$key]);
                }
            }
        }
       
        //=======================(分页显示)
        $info_list_array = array();
        $i = 0;
        foreach ($info_list as $key => $val) {
            $info_list_array[$i]['key'] = $key;
            $info_list_array[$i]['val'] = $val;
            $i++;
        }
        $common = new helper_common();
        $page = $this->getRequest()->getParam("page",1);
        $info_list = $common->page($info_list_array, 10, $page);
        $page = $info_list['page'];
        $pnum  = $info_list['pnum'];
        unset($info_list['page']);
        unset($info_list['pnum']);
        //========================
        $this->_view->info_list = $info_list;
        $this->_view->page = $page;
        $this->_view->pnum = $pnum;
        $this->_layout->meta_title = '功能点列表';
        $default = $Music->get_default();
        $this->_view->default = $default;
    }

    /*
     * add admin page
     *
     */

    public function AddmusicAction() {
        if (!strstr($this->sign['permission'], strtolower($this->request->controller) . '/' . $this->request->action)) {
            header("Content-Type:text/html;charset=utf-8");
            echo "<script>alert('无权限进行此操作，请先联系管理员！');history.back();</script>";
            exit;
        }
        
        $this->_layout->meta_title = '添加功能点';
    }

    /*
     * insert admin data
     *
     */

    public function InsertAction() {
        if (!strstr($this->sign['permission'], strtolower($this->request->controller) . '/' . $this->request->action)) {
            header("Content-Type:text/html;charset=utf-8");
            echo "<script>alert('无权限进行此操作，请先联系管理员！');history.back();</script>";
            exit;
        }
        $data = array(
            'music_name' => $this->getRequest()->getPost('music_name', ''),
            'music_number' => $this->getRequest()->getPost('music_number', ''),
        );

        if (empty($data['music_name']) || empty($data['music_number'])) {
            $_SESSION['message'] = '功能内容不能为空，请重新输入！';
            header("Location:addmusic");
            exit;
        }

        $Music = new MusicModel();
        if($this->getRequest()->getPost('music_default', '')){
            if(!$Music->set_default($data['music_number'])){
                $_SESSION['message'] = '很抱歉，默认显示设置失败，请重新输入！';
                header("Location:addmusic");
            }
        }
        if( $Music->check_val($data['music_name'])){
            $_SESSION['message'] = '很抱歉，此功能名称已存在，请重新输入！';
            header("Location:addmusic");
            exit;
        }
        if ($Music->save($data)) {
            header("Location:index");
        } else {
            $_SESSION['message'] = '很抱歉，此功能代号已存在，请重新输入！';
            header("Location:addmusic");
        }
    }

    /*
     * delete one admin info
     * 
     */

    public function DelAction() {
        if (!strstr($this->sign['permission'], strtolower($this->request->controller) . '/' . $this->request->action)) {
            echo "101:无权限进行此操作，请先联系管理员！";
            exit;
        }

        $id = $this->getRequest()->getPost('id', '');

        if (empty($id)) {
            echo "101:删除失败！请正确选择功能点！";
            exit;
        } else {
            $Music = new MusicModel();
            $row = $Music->del($id);
            if (empty($row)) {
                echo "101:删除失败，请重试！";
                exit;
            } else {
                echo "100:删除成功！";
                exit;
            }
        }
    }

    /*
     * show edit admin info 
     *
     */

    public function EditAction() {
        if (!strstr($this->sign['permission'], strtolower($this->request->controller) . '/' . $this->request->action)) {
            header("Content-Type:text/html;charset=utf-8");
            echo "<script>alert('无权限进行此操作，请先联系管理员！');history.back();</script>";
            exit;
        }
        $Music = new MusicModel();
        $default = $Music->get_default();
        $this->_view->default = $default;
        $id = $this->getRequest()->getParam("id", 0);
        if (!empty($id)) {
            $this->_view->music_info = $Music->get_info($id);
        } else {
            header("Location:index");
        }
        $this->_layout->meta_title = '编辑功能点';
    }

    /*
     * edit admin info 
     *
     */

    public function UpdateAction() {
        if (!strstr($this->sign['permission'], strtolower($this->request->controller) . '/' . $this->request->action)) {
            header("Content-Type:text/html;charset=utf-8");
            echo "<script>alert('无权限进行此操作，请先联系管理员！');history.back();</script>";
            exit;
        }       
        $data = array(
            'music_name' => $this->getRequest()->getPost('music_name', ''),
            'music_number' => $this->getRequest()->getPost('music_number', ''),
        );
        $music_number = $data['music_number'];       
        if (empty($data['music_name'])) {
            $_SESSION['message'] = '功能说明不能为空，请重新输入！';
            header("Location:edit/id/{$music_number}");
            exit;
        }
        $key = $data['music_number'];
        $Music = new MusicModel();
        $default = $Music->get_default();
        $default_value = $this->getRequest()->getPost('music_default', '');
        if(empty($default_value) && $default == $key){
            if(!$Music->set_default('')){
                $_SESSION['message'] = '很抱歉，默认显示取消失败，请重新输入！';
                header("Location:edit/id/{$music_number}");
            }
        }
        if($this->getRequest()->getPost('music_default', '')){
            if(!$Music->set_default($key)){
                $_SESSION['message'] = '很抱歉，默认显示设置失败，请重新输入！';
                header("Location:edit/id/{$music_number}");
            }
        }      
        if( $Music->check_val_update($data)){
            $_SESSION['message'] = '很抱歉，此功能名称已存在，请重新输入！';
            header("Location:edit/id/{$music_number}");
            exit;
        }
        if ($Music->update($data)) {
            header("Location:index");
        } else {
            $_SESSION['message'] = '很抱歉，更新失败，稍后请重试！';
            header("Location:edit/id/{$music_number}");
        }
    }

}
